﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;
using System.Drawing;

namespace School.Pages.Bus
{
    public partial class Buses : System.Web.UI.Page
    {
        private static SchoolEntities db = new SchoolEntities();
        protected void Page_Load(object sender, EventArgs e)
        {
            if ((IQueryable<int>)Session["privilage"] != null)
            {
                var x = (IQueryable<int>)Session["privilage"];

                if (x.Contains(1204) == true)
                {
                    if (!IsPostBack)
                    {
                        ddlBind(ddCamp1, School.Service.Bus.GetAllCampuses().ToList());
                        ddlBind(ddCamp, School.Service.Bus.GetAllCampuses().ToList());
                        gvbusBind();
                    }
                    lbltestBus.Visible = false;
                    lbltestBusse.Visible = false;
                }
                else
                    Response.Redirect("../organization/no_privelege.aspx");
            }
            else
                Response.Redirect("../organization/no_privelege.aspx");
        }
        //CheckName
        [System.Web.Services.WebMethod]
        public static string CheckCode(string args)
        {
            string returnValue = string.Empty;
            try
            {
                var qy = School.Service.Bus.GetBusByCode(args.Trim());
                if (qy.Count() == 0)
                    returnValue = "Available";
                else
                    returnValue = "<img src='../images/n.png'> <font color='#cc0000'><b>'" + args + "'</b> مستخدم.</font></img>";
            }
            catch
            { }
            return returnValue;

        }
        [System.Web.Services.WebMethod]
        public static string CheckName(string args)
        {

            string returnValue = string.Empty;
            try
            {
                var qy = School.Service.Bus.GetBusByName(args.Trim());
                if (qy.Count() == 0)
                    returnValue = "Available";
                else
                    returnValue = "<img src='../images/n.png'> <font color='#cc0000'><b>'" + args + "'</b> مستخدم.</font></img>";
            }
            catch
            { }
            return returnValue;

        }
        [System.Web.Services.WebMethod]
        public static void ddlBind(DropDownList ddl)
        {
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "0"));
            ddl.DataBind();
        }

        private void gvbusBind()
        {

            int count;
            IEnumerable<SRV_Bus> query = School.Service.Bus.GetBus();
            if (ddCamp.SelectedIndex > 0)
                query = query.Where(s => s.Campus_Id == Convert.ToInt32(ddCamp.SelectedValue));
            if (txtNamese.Text.Length > 0)
                query = query.Where(s => s.Name.Contains(txtNamese.Text));
            if (txtcodese.Text.Length > 0)
                query = query.Where(s => s.Bus_Code.Contains(txtcodese.Text));
            if (txtPassengerse.Text.Length > 0)
                query = query.Where(s => s.Passenger_number == Convert.ToInt32(txtPassengerse.Text));
            var gv = (from q in query
                      join camp in db.ORG_Campus on q.Campus_Id equals camp.Id
                      select new
                      {
                          camp.Name,
                          busname = q.Name,
                          q.Bus_Code,
                          q.Passenger_number,
                          q.note,
                          q.Id
                      }).ToList();

            gvbus.DataSource = gv;
            gvbus.DataBind();
            count = gvbus.Rows.Count;
            lblRecordNo.Text = "عدد نتائج البحث = " + count.ToString();
        }
        private void ddlBind(DropDownList ddl, object o)
        {
            ddl.DataSource = o;
            ddl.Items.Clear();
            ddl.Items.Insert(0, new ListItem("اختيار", "-1"));
            ddl.DataBind();
        }


        protected void btnSaveStation_Click(object sender, EventArgs e)
        {
            bool re = School.Service.Bus.AddBus(Convert.ToInt32(ddCamp1.SelectedValue.ToString()), txtcode.Text, txtName.Text, Convert.ToInt32(txtPassenger.Text), txtnote.Text);
            if (re == true)
            {
                lbltestBusse.ForeColor = Color.Green;
                lbltestBusse.Text = "تمت الاضافة بنجاح";
                txtcode.Text = "";
                txtName.Text = "";
                txtnote.Text = "";
                txtPassenger.Text = "";
                ddlBind(ddCamp1, School.Service.Bus.GetAllCampuses().ToList());
                gvbusBind();
            }
            else
            {
                lbltestBusse.ForeColor = Color.Red;
                lbltestBusse.Text = "لم تتم عملية الاضافة..حاول مرة اخري";
            }
            lbltestBusse.Visible = true;
        }

        protected void gvbus_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvbus.EditIndex = e.NewEditIndex;
            gvbusBind();
        }

        protected void gvbus_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvbus.EditIndex = -1;
            gvbusBind();
        }

        protected void gvbus_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int Id = Convert.ToInt32(gvbus.DataKeys[e.RowIndex].Value.ToString());
            bool re = School.Service.Bus.Deletebus(Id);
            if (re == true)
            {
                lbltestBusse.ForeColor = Color.Green;
                lbltestBusse.Text = "تم الحذف بنجاح";
                gvbusBind();
            }
            else
            {
                lbltestBusse.ForeColor = Color.Red;
                lbltestBusse.Text = "لم تتم عملية الحذف..حاول مرة اخري";
            }
            lbltestBusse.Visible = true;
        }

        protected void gvbus_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int Id = Convert.ToInt32(gvbus.DataKeys[e.RowIndex].Value.ToString());
            TextBox Name = (TextBox)gvbus.Rows[e.RowIndex].FindControl("txtbname");
            TextBox Code = (TextBox)gvbus.Rows[e.RowIndex].FindControl("txtbcode");
            TextBox Note = (TextBox)gvbus.Rows[e.RowIndex].FindControl("txtbnote");
            TextBox Passenger = (TextBox)gvbus.Rows[e.RowIndex].FindControl("txtbPassenger");
            bool re = School.Service.Bus.Updatebus(Id, Code.Text, Name.Text, Convert.ToInt32(Passenger.Text), Note.Text);
            if (re == true)
            {
                lbltestBusse.ForeColor = Color.Green;
                lbltestBusse.Text = "تم التحديث بنجاح";
                gvbus.EditIndex = -1;
                gvbusBind();
            }
            else
            {
                lbltestBusse.ForeColor = Color.Red;
                lbltestBusse.Text = "لم تتم عملية التحديث..حاول مرة اخري";
            }
            lbltestBusse.Visible = true;
        }
        protected void btnsearch_Click(object sender, EventArgs e)
        {
            gvbusBind();
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            PLsearch.Visible = false;
            Plview.Visible = true;
        }
        [System.Web.Services.WebMethod]
        public bool Exit()
        {
            ddlBind(ddCamp1, School.Service.Bus.GetAllCampuses().ToList());
            return true;

        }

        protected void gvbus_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton lkdelete = (LinkButton)e.Row.FindControl("lkdelete");
                lkdelete.Attributes.Add("onclick", "javascript:return " +
                "confirm('هل أنت متأكد من عملية الحذف')");
            }
        }

        protected void gvbus_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvbus.PageIndex = e.NewPageIndex;
            gvbusBind();
        }

    }
}